﻿package  {
	
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.FlxCollision;
	import org.flixel.plugin.photonstorm.FlxBar;
	import org.flixel.plugin.photonstorm.FlxWeapon;
	
	public class GeneralPlayState extends FlxState {
		[Embed(source="data/fire.png")] private var ImgGibs:Class;

		public var player:Player;
		public var playerHealthBar:FlxBar;
		
		public var chainsaw:Chainsaw;
		public var weapons:FlxGroup;
		
		public var damagersensible:FlxGroup;		
		public var fire:FlxWeapon;

		public var scrollmessage:Number;
		public var generalmessage:FlxText;
		public var healthmessage:FlxText;
		
		public function isDoorCollide(door:FlxSprite):Boolean
		{
			if(FlxG.keys.UP)
			  return FlxCollision.pixelPerfectCheck(door,player);
			else return false;  
		}


		public function hurt(contact:FlxBasic, target:FlxBasic, alphaTolerance:int = 255, camera:FlxCamera = null):Boolean
		{
			var members:Array;	
			var i:uint;
			
			if((contact == null)||(target == null))return false;
			if((contact is FlxSprite)&&(!contact.exists))return false;
			if((target is FlxSprite)&&(!target.exists))return false;
			
			if(contact is FlxGroup){
				i = 0;
				members = (contact as FlxGroup).members;
				while(i < members.length)
					hurt(members[i++],target,alphaTolerance,camera);
				return true;
				}

			if(target is FlxGroup){
				i = 0;
				members = (target as FlxGroup).members;
				while(i < members.length)
					hurt(contact,members[i++],alphaTolerance,camera);
				return true;
				}
			
			if(FlxCollision.pixelPerfectCheck(contact as FlxSprite,target as FlxSprite,100,camera))
				if(contact is Chainsaw)
				(target as FlxSprite).hurt((contact as Chainsaw).damage);
				else 
				(target as FlxSprite).hurt(1);
			
			return true;	
		}



		private function CollisionsHurt(player:Player, chainsaw:Chainsaw, damagersensible:FlxGroup):void
		{
				hurt(chainsaw, damagersensible);
				hurt(fire.group, damagersensible);
				hurt(damagersensible, player);
				
		}
		
		override public function create():void
		{	
			scrollmessage = -1;

			weapons = new FlxGroup();
		    damagersensible = new FlxGroup();		
			
			chainsaw = new Chainsaw(32,176,1);
			weapons.add(chainsaw);
			add(chainsaw);
			
			player = new Player(50,50);
			add(player);
			
			fire = new FlxWeapon("lazer2", player, "x", "y"); 
			fire.makeImageBullet(50, ImgGibs,15,15);
			fire.setBulletSpeed(100);
			fire.setBulletLifeSpan(1000);
			fire.setBulletBounds(new FlxRect(0, 0, 2500, 240));
			add(fire.group);
			
			healthmessage =  new FlxText(20+FlxG.camera.scroll.x,20,236,player.health.toString());
			add(healthmessage);
			
			generalmessage = new FlxText(32+FlxG.camera.scroll.x,36,236,"").setFormat(null,16,0xb21e24,"center");
			add(generalmessage)
			
			FlxG.camera.follow(player, FlxCamera.STYLE_LOCKON);
	
		}
	
	
		override public function destroy():void
		{
			super.destroy();
			player = null;
			chainsaw = null;
			weapons = null;
			damagersensible = null;
		}
	
		override public function update():void
		{
			super.update();
			FlxG.collide();
			CollisionsHurt(player, chainsaw, damagersensible);

			chainsaw.frame = player.frame;
			chainsaw.x = player.x;
			chainsaw.y = player.y;
			chainsaw.facing = player.facing;
			chainsaw.visible = player.visible;
			chainsaw.alive = player.alive;
			chainsaw.exists = player.exists;
	
			if((chainsaw.frame >= 0)&&(chainsaw.frame <= 5))
			 chainsaw.damage = 0;
			else chainsaw.damage = 1; 
	
			if(player.firing)
			{
			  if(player.facing == FlxObject.RIGHT)
			  	{
				fire.setBulletOffset(15,15);	
				fire.fireAtPosition(player.x + 20, player.y+18);
				}
			  if(player.facing == FlxObject.LEFT)
				{
				fire.setBulletOffset(-15,15);
				fire.fireAtPosition(player.x - 20, player.y+18);	
				}
			  player.firing = false;
			}
	

			if(player.health == 0)healthmessage.text = "";
			if(player.health == 1)healthmessage.text = "*";
			if(player.health == 2)healthmessage.text = "**";
			if(player.health == 3)healthmessage.text = "***";
			if(player.health == 4)healthmessage.text = "****";
			if(player.health == 5)healthmessage.text = "*****";
			healthmessage.x = 20+FlxG.camera.scroll.x
			
	
			generalmessage.x = FlxG.camera.scroll.x;
			if(!player.alive){
				generalmessage.text = "YOU DIED     press Enter to continue";}
				if(FlxG.keys.ENTER)
				  FlxG.resetState();

			}
		}
	
	
	
}
